package net.minecraft.src;

import java.util.Random;
import java.io.*;
import net.minecraft.client.Minecraft;

public class BlockNetherrackFurnace extends BlockContainer{
    protected BlockNetherrackFurnace(int i, boolean flag)
    {
        super(i, Material.rock);
        furnaceRand = new Random();
        isActive = flag;
        setTickOnLoad(true);
        blockIndexInTexture = mod_NetherrackFurnace.texfurnaceside;
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("--Block-started");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    protected boolean canThisPlantGrowOnThisBlockID(int i)
    {
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-canThisPlantGrowOnThisBlockID");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return i == Block.grass.blockID || i == Block.dirt.blockID || i == Block.stone.blockID;
    }

    public boolean canBlockStay(World world, int i, int j, int k)
    {
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-canBlockStay");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return canThisPlantGrowOnThisBlockID(world.getBlockId(i, j - 1, k));
    }

    public void updateTick(World world, int i, int j, int k, Random random)
    {
        if(random.nextInt(1) == 0 && blockID == mod_NetherrackFurnace.furnaceActive.blockID)
        {
            int l = (i + random.nextInt(3)) - 1;
            int j1 = (j + random.nextInt(2)) - random.nextInt(2);
            int l1 = (k + random.nextInt(3)) - 1;
            if(world.isAirBlock(l, j1, l1) && canBlockStay(world, l, j1, l1))
            {
                i += random.nextInt(3) - 1;
                k += random.nextInt(3) - 1;
            }
        }
        if(random.nextInt(80) == 0 && blockID == mod_NetherrackFurnace.furnaceActive.blockID)
        {
            int i1 = (i + random.nextInt(3)) - 1;
            int k1 = (j + random.nextInt(2)) - random.nextInt(2);
            int i2 = (k + random.nextInt(3)) - 1;
        }
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-updateTick");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public int idDropped(int i, Random random)
    {
        /*        try
        {
            //debug log
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-idDropped");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return mod_NetherrackFurnace.furnaceIdle.blockID;
    }

    public void onBlockAdded(World world, int i, int j, int k)
    {
        super.onBlockAdded(world, i, j, k);
        setDefaultDirection(world, i, j, k);
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-onBlockAdded");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    private void setDefaultDirection(World world, int i, int j, int k)
    {
        if(world.multiplayerWorld)
        {
            return;
        }
        int l = world.getBlockId(i, j, k - 1);
        int i1 = world.getBlockId(i, j, k + 1);
        int j1 = world.getBlockId(i - 1, j, k);
        int k1 = world.getBlockId(i + 1, j, k);
        byte byte0 = 3;
        if(Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
        {
            byte0 = 3;
        }
        if(Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
        {
            byte0 = 2;
        }
        if(Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
        {
            byte0 = 5;
        }
        if(Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
        {
            byte0 = 4;
        }
        world.setBlockMetadataWithNotify(i, j, k, byte0);
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-setDefaultDirection");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public int getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l)
    {
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-getBlockTexture");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(l == 1)
        {
            return mod_NetherrackFurnace.texfurnacetop;
        }
        if(l == 0)
        {
            return mod_NetherrackFurnace.texfurnacetop;
        }
        int i1 = iblockaccess.getBlockMetadata(i, j, k);
        if(l != i1)
        {
            return blockIndexInTexture;
        }
        if(isActive)
        {
            return mod_NetherrackFurnace.texfurnacefrontactive;
        } else
        {
            return mod_NetherrackFurnace.texfurnacefrontidle;
        }
    }

    public void randomDisplayTick(World world, int i, int j, int k, Random random)
    {
        if(!isActive)
        {
            return;
        }
        int l = world.getBlockMetadata(i, j, k);
        float f = (float)i + 0.5F;
        float f1 = (float)j + 0.0F + (random.nextFloat() * 6F) / 16F;
        float f2 = (float)k + 0.5F;
        float f3 = 0.52F;
        float f4 = random.nextFloat() * 0.6F - 0.3F;
        if(l == 4)
        {
            world.spawnParticle("smoke", f - f3, f1, f2 + f4, 0.0D, 0.0D, 0.0D);
        } else
        if(l == 5)
        {
            world.spawnParticle("smoke", f + f3, f1, f2 + f4, 0.0D, 0.0D, 0.0D);
        } else
        if(l == 2)
        {
            world.spawnParticle("smoke", f + f4, f1, f2 - f3, 0.0D, 0.0D, 0.0D);
        } else
        if(l == 3)
        {
            world.spawnParticle("smoke", f + f4, f1, f2 + f3, 0.0D, 0.0D, 0.0D);
        }
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-randomDisplayTick");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public int getBlockTextureFromSide(int i)
    {
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-getBlockTexturFromSide");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(i == 1)
        {
            return mod_NetherrackFurnace.texfurnacetop;
        }
        if(i == 0)
        {
            return mod_NetherrackFurnace.texfurnacetop;
        }
        if(i == 3)
        {
            return mod_NetherrackFurnace.texfurnacefrontidle;
        } else
        {
            return blockIndexInTexture;
        }
    }

    public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
    {
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-blockActivated");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        if(world.multiplayerWorld)
        {
            return true;
        } else
        {
            TileEntityNetherrackFurnace tileentitynetherrackfurnace = (TileEntityNetherrackFurnace)world.getBlockTileEntity(i, j, k);//!@$%^
            ModLoader.OpenGUI(entityplayer, new GuiNetherrackFurnace(entityplayer.inventory, tileentitynetherrackfurnace));//@$@#^
            return true;
        }
    }

    public static void updateFurnaceBlockState(boolean flag, World world, int i, int j, int k)
    {
        int l = world.getBlockMetadata(i, j, k);
        TileEntity tileentity = world.getBlockTileEntity(i, j, k);
        keepFurnaceInventory = true;
        if(flag)
        {
            world.setBlockWithNotify(i, j, k, mod_NetherrackFurnace.furnaceActive.blockID);
        } else
        {
            world.setBlockWithNotify(i, j, k, mod_NetherrackFurnace.furnaceIdle.blockID);
        }
        keepFurnaceInventory = false;
        tileentity.validate();
        world.setBlockMetadataWithNotify(i, j, k, l);
        world.setBlockTileEntity(i, j, k, tileentity);
    }

    protected TileEntity getBlockEntity()
    {
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());
            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-getBlockEntity");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        return new TileEntityNetherrackFurnace();
    }

    public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving)
    {
        int l = MathHelper.floor_double((double)((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
        if(l == 0)
        {
            world.setBlockMetadataWithNotify(i, j, k, 2);
        }
        if(l == 1)
        {
            world.setBlockMetadataWithNotify(i, j, k, 5);
        }
        if(l == 2)
        {
            world.setBlockMetadataWithNotify(i, j, k, 3);
        }
        if(l == 3)
        {
            world.setBlockMetadataWithNotify(i, j, k, 4);
        }
        //debug log
        /*        try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-onBlockPlacedBy");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
    }

    public void onBlockRemoval(World world, int i, int j, int k)
    {
        if(!keepFurnaceInventory)
        {
            TileEntityNetherrackFurnace tileentitynetherrackfurnace = (TileEntityNetherrackFurnace)world.getBlockTileEntity(i, j, k); //SDTEWT
label0:
            for(int l = 0; l < tileentitynetherrackfurnace.getSizeInventory(); l++)
            {
                ItemStack itemstack = tileentitynetherrackfurnace.getStackInSlot(l);
                if(itemstack == null)
                {
                    continue;
                }
                float f = furnaceRand.nextFloat() * 0.8F + 0.1F;
                float f1 = furnaceRand.nextFloat() * 0.8F + 0.1F;
                float f2 = furnaceRand.nextFloat() * 0.8F + 0.1F;
                do
                {
                    if(itemstack.stackSize <= 0)
                    {
                        continue label0;
                    }
                    int i1 = furnaceRand.nextInt(21) + 10;
                    if(i1 > itemstack.stackSize)
                    {
                        i1 = itemstack.stackSize;
                    }
                    itemstack.stackSize -= i1;
                    EntityItem entityitem = new EntityItem(world, (float)i + f, (float)j + f1, (float)k + f2, new ItemStack(itemstack.itemID, i1, itemstack.getItemDamage()));
                    float f3 = 0.05F;
                    entityitem.motionX = (float)furnaceRand.nextGaussian() * f3;
                    entityitem.motionY = (float)furnaceRand.nextGaussian() * f3 + 0.2F;
                    entityitem.motionZ = (float)furnaceRand.nextGaussian() * f3;
                    world.entityJoinedWorld(entityitem);
                } while(true);
            }
//debug log
         /*       try
        {
            
            File log = new File((new StringBuilder()).append(Minecraft.getMinecraftDir()).append("/mod_NetherrackFurnace.log").toString());

            FileWriter fw = new FileWriter(log,true);
            PrintWriter pw=new PrintWriter(fw);
            pw.println("Block-onBlockRemoval");
            pw.close();
        }
                catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }*/
        }
        super.onBlockRemoval(world, i, j, k);
    }

    private Random furnaceRand;
    private final boolean isActive;
    private static boolean keepFurnaceInventory = false;
}
